var MedicineValidation = function () {

    var handleValidation1 = function() {
        // for more info visit the official plugin documentation: 
            // http://docs.jquery.com/Plugins/Validation

            var form1 = $('#form_medicine');
            var error1 = $('.alert-danger', form1);
            var success1 = $('.alert-success', form1);

            form1.validate({
                errorElement: 'span', //default input error message container
                errorClass: 'help-block', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                	no: {
                        required: true
                    },
                    name: {
                        required: true
                    },
                    type: {
                        required: true
                    },
                    purchase_price: {
                        required: false,
                        digits: true
                    },
                    retail_price: {
                    	 required: false,
                         digits: true
                    }              
                },

                invalidHandler: function (event, validator) { //display error alert on form submit              
                    success1.hide();
                    error1.show();
                    App.scrollTo(error1, -200);
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.form-group').addClass('has-error'); // set error class to the control group
                },

                unhighlight: function (element) { // revert the change done by hightlight
                    $(element)
                        .closest('.form-group').removeClass('has-error'); // set error class to the control group
                },

                success: function (label) {
                    label
                        .closest('.form-group').removeClass('has-error'); // set success class to the control group
                    
                },

                submitHandler: function (form) {
                	$.ajax({
        	            type:"post",
        	            url:"/pims/admin/medicine!save.action",
        	            dataType:"json",
        	            data:"no="+$("#Me_no").val()+"&name="+$("#Me_name").val()+"&type="+$("#Me_type").val()+
        	            	 "&su_name="+$("#Su_name").val()+"&purchase_price="+$("#Me_purchase_price").val()+
        	            	 "&retail_price="+$("#Me_retail_price").val()+"&introduce="+$("#Me_introduce").val()+
        	            	 "&prodate="+$("#Me_prodate").val()+"&exdate="+$("#Me_exdate").val()+
        	            	 "&remarks="+$("#Me_remarks").val()+"&id="+$("#Me_id").val(),
        	            cache:false,
        	            beforeSend:function(XMLHttpRequest){
        	            },
        	            success:function(json){
        	            	success = json.success;
        	            	if(success){
        	            		if(confirm("操作成功,点击'确定'返回列表,点击'取消'返回添加界面!") == true){
        	            			window.location.href="/pims/admin/medicine!list.action";
        	            		}else{
        	            			window.location.href="/pims/admin/medicine!add.action";
        	            		}
        	            	}else{
        	            		alert("操作失败,请稍后重试!");
        	            	}
        	                
        	            },
        	           	complete:function(XMLHttpRequest,textStatus){
        	           	},
        	          	error:function(XMLHttpRequest,textStatus,errorThrown){
        	                   alert("网络错误,请稍后重试!");
        	          	}
                    });
                    error1.hide();
                }
            });

    }

    var handleWysihtml5 = function() {
        if (!jQuery().wysihtml5) {
            
            return;
        }

        if ($('.wysihtml5').size() > 0) {
            $('.wysihtml5').wysihtml5({
                "stylesheets": ["assets/plugins/bootstrap-wysihtml5/wysiwyg-color.css"]
            });
        }
    }

    return {
        //main function to initiate the module
        init: function () {

            handleWysihtml5();
            handleValidation1();
        }

    };

}();